<?php

namespace app\admin\controller;

use think\Db;
use think\Request;
use think\cache\driver\Redis;
class Salepacker extends Auth
{

	public function __construct(Request $request){
        parent::__construct($request);
        $this->data = $request->param();
        $this->lang = isset($this->data['lang']) && $this->data['lang'] ? $this->data['lang'] : is_lang();
        $this->user_id = session('userid') == null ? 1 : session('userid');
        $this->getChildIDdata = [];
        $this->saleModel = model('SaleModel');
        $this->AdminWarehouse_id = $this->saleModel->getAdminWarehouse($this->user_id);
    }


    // 
    public function list(Request $request){
        if(isset($this->data['request']) && $this->data['request'] == 'requestAjax'){

            $page = "1,1000000";
            $return['page'] = 1;
            $return['pageSum'] = 0;
            $pageSize = 1000000;
            if(isset($this->data['page']) && isset($this->data['pageSize'])){
                $page = $this->data['page'].','.$this->data['pageSize'];
                $return['page'] = $this->data['page'];
                $pageSize = $this->data['pageSize'];
            }

            $where['warehouse_id'] = $this->AdminWarehouse_id;
            if( isset($this->data['myWork']) && $this->data['myWork'] == 1) $where['create_id'] = $this->user_id;

            // 新订单
            if($this->data['requestType'] == 'list'){
                $where['process_id'] = 0;
                $return['list'] = $this->getList($where,$page);
                $return['pageSum'] = hasData('make_process_detail',$where);
            }else if($this->data['requestType'] == 'process' || $this->data['requestType'] == 'pause' || $this->data['requestType'] == 'finish' || $this->data['requestType'] == 'cancel' ){
                $where['start_at'] = ['gt',0];
                $where['pause_at'] = 0;
                $where['finish_at'] = 0;
                $where['cancel_at'] = 0;
                if($this->data['requestType'] == 'pause') $where['pause_at'] = ['gt',0];
                if($this->data['requestType'] == 'finish') $where['finish_at'] = ['gt',0];
                if($this->data['requestType'] == 'cancel') $where['cancel_at'] = ['gt',0];
                $returnData = $this->getListData($where,$page);
                $return['list'] = $this->saleModel->getPagination($returnData,$pageSize,$return['page']);
                $return['pageSum'] = $returnData['pageSum'];
            } else if($this->data['requestType'] == 'pickup'){  // 提货
                $return['list'] = $this->getPickupData($page);
                $return['pageSum'] = hasData('make_pickup_detail',['warehouse_id'=>$this->AdminWarehouse_id,'start_at'=>0]);
            }

            // dump($return);die;

            return json_encode(array('status'=>1,'data'=>$return));
        }else{
            return view('list',['isshow'=>1]);
        }
    }

    function getNavPageNum()
    {   

        $return['listNum']   = $this->getNavPageNum_('list');
        $return['processNum']= $this->getNavPageNum_('process');
        $return['pauseNum']  =  $this->getNavPageNum_('pause');
        $return['finishNum'] = $this->getNavPageNum_('finish');
        $return['cancelNum'] = $this->getNavPageNum_('cancel');
        $return['pickupNum'] = $this->getNavPageNum_('pickup');
        return json_encode($return);
    }

    function getNavPageNum_($type)
    {
        if( isset($this->data['myWork']) && $this->data['myWork'] == 1) $where['create_id'] = $this->user_id;
        if($type == 'list'){
            $where['warehouse_id'] = $this->AdminWarehouse_id;
            $where['process_id'] = 0;
            $res = hasData('make_process_detail',$where);
        }else if($type == 'pickup'){
            $where['warehouse_id'] = $this->AdminWarehouse_id;
            $where['start_at'] = 0;
            $res = hasData('make_pickup_detail',$where);
        }else{
            $this->data['requestType'] = $type;
            $where['start_at'] = ['gt',0];
            $where['pause_at'] = 0;
            $where['finish_at'] = 0;
            $where['cancel_at'] = 0;
            if($type == 'pause') $where['pause_at'] = ['gt',0];
            if($type == 'finish') $where['finish_at'] = ['gt',0];
            if($type == 'cancel') $where['cancel_at'] = ['gt',0];
            $res = $this->getListData($where,'1,10000000')['pageSum'];
        }
        return $res;

    }

    function getListData($where,$page)
    {
        $list = getDatas('make_process',['*'],$where,'id desc');
        $pageSum = 0;
        if(!empty($list)){
            foreach ($list as $k => $v) {
                $where_['process_id'] = $v['id'];
                if($this->data['requestType'] == 'pause') $where_['pause_at'] = ['gt',0];
                if($this->data['requestType'] == 'finish') $where_['finish_at'] = ['gt',0];
                if($this->data['requestType'] == 'cancel') $where_['cancel_at'] = ['gt',0];
                $list[$k]['team_names'] = $this->saleModel->getTeam_names($v['team_ids']);

                $list[$k]['detail'] = getDatas('make_process_detail',['*'],$where_);
                $pageSum += count($list[$k]['detail']);

                $list[$k]['create_name'] = getValue('admin',['id'=>$v['create_id']],'nickname');
                $list[$k]['create_time'] = getTimeDates($v['create_at'],$this->lang);
                $list[$k]['finish_name'] = isset($v['finish_id']) ? getValue('admin',['id'=>$v['finish_id']],'nickname') : '-';
                $list[$k]['finish_time'] = $v['finish_at'] ? getTimeDates($v['finish_at'],$this->lang) : '';

                $list[$k]['pause_name'] = getValue('admin',['id'=>$v['pause_id']],'nickname');
                $list[$k]['pause_time'] = $v['pause_at'] ? getTimeDates($v['pause_at'],$this->lang) : '' ;

                $list[$k]['cancel_name'] = getValue('admin',['id'=>$v['cancel_id']],'nickname');
                $list[$k]['cancel_time'] = $v['cancel_at'] ? getTimeDates($v['cancel_at'],$this->lang) : '';

                $list[$k]['qtySum'] = 0;
                $list[$k]['remark'] = getDatas('make_process_remark',['*'],['process_id'=>$v['id']]);
                foreach ($list[$k]['detail'] as $k1 => $v1) {
                    $list[$k]['detail'][$k1]['proInfo'] = getProAndProchar($v1,$this->lang,48);
                    $list[$k]['detail'][$k1]['storeInfo'] = $this->getstore2area($v1['pro_id'],$v1['prochar_id'],$v1['warehouse_id'],$list[$k]['detail'][$k1]['proInfo']['prochar_qty'],$this->lang); 
                    $list[$k]['qtySum'] += $v1['qty'];
                    $list[$k]['detail'][$k1]['remark'] = getDatas('make_process_remark',['*'],['detail_id'=>$v1['id']]);
                    $list[$k]['detail'][$k1]['checker_name'] =  getValue('admin',['id'=>getValue('sale_make',['id'=>$v1['make_id']],'checker_id')],'nickname');
                }
            }
        }
        $returnData['list'] = $list;
        $returnData['pageSum'] = $pageSum;
        return $returnData;
    }

    public function getList($where,$page)
    {   
        $list = getDatas('make_process_detail',['*'],$where,'priority desc',$page);
        // dump($list);die;
        $returnData = [];
        if(!empty($list)){
            $j=0;
            $sale_ids = '';
            $key = [];
            foreach ($list as $k => $v) {
                $res['sale_id'] = $v['sale_id'];
                $res['make_id'] = $v['make_id'];
                $detail['create_name'] = getValue('admin',['id'=>$v['create_id']],'nickname');
                $detail['create_time'] = getTimeDates($v['create_at'],$this->lang);
                $detail['create_at'] = $v['create_at'];
                $detail['priority'] = $v['priority'];
                $detail['detail_id'] = $v['id'];
                $detail['sale_make'] = Db::name('sale_make')->where(['id'=>$v['make_id']])->find();
                $detail['proInfo'] = getProAndProchar($v,$this->lang,48);
                $detail['remark'] = getDatas('make_process_remark',['*'],['detail_id'=>$v['id']]);
                $detail['checker_name'] =  getValue('admin',['id'=>getValue('sale_make',['id'=>$v['make_id']],'checker_id')],'nickname');
                $detail['qty'] = $v['qty'];
                $detail['storeInfo'] = $this->getstore2area($v['pro_id'],$v['prochar_id'],$v['warehouse_id'],$detail['proInfo']['prochar_qty'],$this->lang);    

                if(empty($returnData) || !$v['sale_id']){
                    $returnData[$j] = $res;
                    $returnData[$j]['detail'][] = $detail;
                    if($v['sale_id']){
                        $sale_ids .= $v['sale_id'].',';
                        $key[$v['sale_id']] = $j;
                    } 
                    $j++;
                }else{
                    if(!strstr($sale_ids,(String)$v['sale_id'])){
                        $returnData[$j] = $res;
                        $returnData[$j]['detail'][] = $detail;
                        if($v['sale_id']){
                            $sale_ids .= $v['sale_id'].',';
                            $key[$v['sale_id']] = $j;
                        } 
                        $j++;
                    }else{
                        // $returnData[$key[$v['sale_id']]][] = $res;
                        $returnData[$key[$v['sale_id']]]['detail'][] = $detail;
                    }
                }

            }
        }

        return $returnData;
    }

    function getPickupData($page)
    {
        $list = getDatas('make_pickup_detail',['*'],['warehouse_id'=>$this->AdminWarehouse_id,'start_at'=>0]);
        if(!empty($list)){
            foreach ($list as $k => $v) {
                $list[$k]['proInfo'] = getProAndProchar($v,$this->lang,48);
                $list[$k]['storeInfo'] = $this->getstore2area($v['pro_id'],$v['prochar_id'],$v['warehouse_id'],$list[$k]['proInfo']['prochar_qty'],$this->lang); 
                $target_name = $v['target'] == 0 ? '销售订单' : $v['target'] == 1 ? '散货' : $v['target'] == 2 ? '包装部' : '';
                $list[$k]['status_name'] = '-';
                $list[$k]['status_name'] = $v['start_at'] > 0 ? '提货中' : $list[$k]['status_name'];
                $list[$k]['status_name'] = $v['cancel_at'] > 0 ? '已取消' : $list[$k]['status_name'];  
                $list[$k]['status_name'] = $v['pause_at'] >0 ? '暂停中' : $list[$k]['status_name']; 
                $list[$k]['status_name'] = $v['finish_at'] > 0 ? '完成' : $list[$k]['status_name']; 
                $list[$k]['status_name'] = $v['received_at'] > 0 ? '已收货' : $list[$k]['status_name']; 
                $list[$k]['area_name'] = $target_name.getValue('warehouse_area',['id'=>$v['area_id']],'name');
                $checker_id = getValue('sale_make',['id'=>$v['make_id']],'checker_id');
                $list[$k]['checker_name'] = $checker_id ? getValue('admin',['id'=>$checker_id],'nickname') : '-';
                $list[$k]['create_name'] = getValue('admin',['id'=>$v['create_id']],'nickname');
                $list[$k]['create_time'] = getTimeDates($v['create_at'],$this->lang);
            }
        }
        return $list;
    }

    function getstore2area($pro_id,$prochar_id,$warehouse_id,$lang)
    {
        // $where['zu.type_id']=3;
        // $where['a.pro_id'] = $pro_id;
        // $where['a.prochar_id'] = $prochar_id;
        // $where['a.warehouse_id'] = $warehouse_id;
        // $data = getSearch('warehouse_area','zu',['zu.id,zu.name,a.id store2area_id,a.qty,a.volume'],$where,[['store2area a','zu.id = a.area_id','LEFT']],'a.qty asc');
        $data = getDatas('warehouse_area',['id,name'],['type_id'=>3,'warehouse_id'=>$warehouse_id]);
        if(!empty($data)){
            foreach ($data as $k => $v) {
                $store2area = Db::name('store2area')->where(['pro_id'=>$pro_id,'prochar_id'=>$prochar_id,'warehouse_id'=>$warehouse_id,'area_id'=>$v['id']])->find();
                if(!empty($store2area)){
                    $data[$k]['qty'] = $store2area['qty'];
                    $data[$k]['volume'] = $store2area['volume'];
                    $data[$k]['store2area_id'] = $store2area['id'];
                }else{
                    $data[$k]['qty'] = 0;
                    $data[$k]['volume'] = 0;
                    $data[$k]['store2area_id'] = 0;
                }
                
            }
        }
        // dump($data);die;
        return $data;
    }

    function startProcess()
    {
        if($this->data['requestType'] == 'get'){
            $returnData = $this->getProItem($this->data,$this->lang);
            return json_encode($returnData);
        }else if($this->data['requestType'] == 'save'){
            return json_encode($this->saveMake_process($this->data));
        }else if($this->data['requestType'] == 'getStore'){
            return getValue('store',['pro_id'=>$this->data['pro_id'],'prochar_id'=>$this->data['prochar_id'],'warehouse_id'=>0],'qty');     
        }

    }

    // 获取配件
    function getProItem($data,$lang,$type=2)
    {
        // dump($data);die;
        $where = ['pro_id'=>$data['pro_id'],'prochar_id'=>$data['prochar_id'],'pm.lang'=>$lang];
        $join = [
                    ['pro p','zu.pro_id2item = p.id'],
                    ['multi_lang pm','p.mkey = pm.mkey']
                ];
        $returnData['item'] = getSearch('pro_item','zu',['zu.*,pm.content itemName'],$where,$join,'zu.id desc');
        if(!empty($returnData['item'])){
            foreach ($returnData['item'] as $k => $v) {
                $returnData['item'][$k]['proInfo'] = getProAndProchar(['pro_id'=>$v['pro_id2item'],'prochar_id'=>$v['prochar_id2item']],$lang,48);
                $returnData['item'][$k]['storeInfo'] = $this->getstore2area($v['pro_id2item'],$v['prochar_id2item'],$data['warehouse_id'],$lang);
            }
        }
        if($type == 2){
            // 获取包装工，和未完成的工作量
            $returnData['packer'] = getDatas('admin',['id,nickname'],['position_id'=>['in','41,42,43']]);
            if(!empty($returnData['packer'])){
                foreach ($returnData['packer'] as $k1=> $v1) {
                    $returnData['packer'][$k1]['workSum'] = Db::name('make_process')->where("find_in_set(".$v1['id'].", team_ids)")->where(['finish_at'=>0])->count();
                }
            }    
        }
        
        return $returnData;
    }

    // 开始处理保存
    function saveMake_process($data)
    {   
        // dump($data);die;
        if($data['qty'] > 0){
            $make_process['qty']          = $data['qty'];
            $make_process['team_ids']     = $data['packer'];
            $make_process['make_id']      = $data['startProcessData']['sale_make']['id'];
            $make_process['warehouse_id'] = $data['startProcessData']['sale_make']['warehouse_id'];
            $make_process['start_at']     = time();
            $make_process['create_at']    = time();
            $make_process['create_id']    = $this->user_id;
            $process_id = insertData('make_process',$make_process);
            if($process_id){
                foreach ($data['detail_id'] as $k1 => $v1) {
                    $res1 = updateData('make_process_detail',['id'=>$v1],['process_id'=>$process_id]);
                    $this->saleModel->addSaleLog(getValue('make_process_detail',['id'=>$v1],'sale_id'),'makePacker','包装主管开始处理');
                }
            }
        
            $make_pickup_detail['warehouse_id'] = $data['startProcessData']['sale_make']['warehouse_id'];
            $make_pickup_detail['start_at']     = time();
            $make_pickup_detail['create_at']    = time();
            $make_pickup_detail['create_id']    = $this->user_id;
            $make_pickup_detail['target'] = 2; 
            // $make_pickup_detail['area_id']      = $data['pickupData']['store2area_id']; 
            // $make_pickup_detail['priority']     = $data['pickupData']['priority']; 

            if(isset($data['pickupData']) && $data['pickupData']['store2area_id'] > 0){
                $make_pickup_detail['pro_id']       = $data['pickupData']['pro_id']; 
                $make_pickup_detail['prochar_id']   = $data['pickupData']['prochar_id']; 
                $make_pickup_detail['qty']          = $data['pickupData']['pickupQty'];
                $make_pickup_detail['area_id']      = $data['pickupData']['store2area_id']; 
                $make_pickup_detail['priority']     = $data['pickupData']['priority']; 
                $res2 = insertData('make_pickup_detail',$make_pickup_detail);
            }

            if(isset($data['pickupItemData'])){
                foreach ($data['pickupItemData'] as $k => $v) {
                    $make_pickup_detail['pro_id'] = $v['pro_id'];
                    $make_pickup_detail['prochar_id'] = $v['prochar_id'];
                    $make_pickup_detail['qty'] = $v['qty'];
                    $make_pickup_detail['priority']     = $data['pickupData']['priority']; 
                    $make_pickup_detail['area_id']      = $data['pickupData']['store2area_id']; 
                    $res3 = insertData('make_pickup_detail',$make_pickup_detail);

                    foreach ($data['detail_id'] as $k2 => $v2) {
                        $make_process_detail_item['warehouse_id'] = $data['startProcessData']['sale_make']['warehouse_id'];
                        $make_process_detail_item['make_id'] = $data['startProcessData']['sale_make']['id'];
                        $make_process_detail_item['process_id'] = $process_id;
                        $make_process_detail_item['process_detail_id'] = $v2;
                        $make_process_detail_item['pro_id'] = $v['pro_id'];
                        $make_process_detail_item['prochar_id'] = $v['prochar_id'];
                        $make_process_detail_item['qty'] = $v['qty'];
                        insertData('make_process_detail_item',['']);
                    }
                }
            }
            if($process_id){
                return array('status'=>1,'msg'=>'处理成功');
            }else{
                return array('status'=>2,'msg'=>'处理失败');
            }
        }else{
            return array('status'=>2,'msg'=>'处理失败');
        }
    }
    //  开始处理暂停
    function listPause()
    {
        if($this->data['requestType'] == 'save'){
            return $this->savePause('list');
        }else{
            $data = $this->getProItem($this->data,$this->lang,1);
            if(!empty($data)){
                foreach ($data['item'] as $k => $v) {
                    $data['item'][$k]['itemStoreSum'] = 0;
                    if(!empty($v['storeInfo'])){
                        foreach ($v['storeInfo'] as $k1 => $v1) {
                            $data['item'][$k]['itemStoreSum'] += $v1['qty'];
                        }
                    }
                }
            }

            // 获取受影响的订单
            // $pickupDetail = getDatas('make_pickup_detail',['sale_id,qty,priority,pro_id,prochar_id'],['pro_id'=>$this->data['pro_id'],'prochar_id'=>$this->data['prochar_id'],'finish_at'=>0,'sale_id'=['gt',0]]);
            $pickupDetail = getSearch('make_pickup_detail','zu',['zu.sale_id,zu.qty,zu.priority,zu.pro_id,zu.prochar_id,s.id sale_detail_id'],['zu.pro_id'=>$this->data['pro_id'],'zu.prochar_id'=>$this->data['prochar_id'],'s.pro_id'=>$this->data['pro_id'],'s.prochar_id'=>$this->data['prochar_id']],[['sale_detail s','zu.sale_id = s.sale_id']],'zu.id desc');
            if(!empty($pickupDetail)){
                foreach ($pickupDetail as $key => $value) {
                    $pickupDetail[$key]['remark'] = getDatas('sale_remark',['*'],['id'=>$value['sale_detail_id']]);
                }
            }

            $data['saleData'] = $pickupDetail;
            return json_encode($data);
        }
    }

    // 处理中查看
    function processCheck()
    {
        return json_encode($this->getProItem($this->data,$this->lang));
    }

    // 处理中 提货保存
    function processPickup()
    {   
        if(isset($this->data['area_id'])){
            // 提交数量为0，则删除之前提交的提货申请
            if($this->data['qty'] > 0 ){

                $make_pickup_detai_where['pro_id'] = $this->data['pro_id'];
                $make_pickup_detai_where['make_id'] = $this->data['make_id'];
                $make_pickup_detai_where['sale_id'] = $this->data['sale_id'];
                $make_pickup_detai_where['prochar_id'] = $this->data['prochar_id'];
                $make_pickup_detai_where['warehouse_id'] = $this->data['warehouse_id'];
                $make_pickup_detai_where['start_at'] = 0;
                $make_pickup_detai_where['area_id'] = $this->data['area_id'];
                $make_pickup_detai_where['priority'] = $this->data['priority'];
                if(hasData('make_pickup_detail',$make_pickup_detai_where)){
                    $res = updateData('make_pickup_detail',$make_pickup_detai_where,['qty'=>$this->data['qty']]);
                }else{
                    $make_pickup_detai_where['create_at'] = time();
                    $make_pickup_detai_where['create_id'] = $this->user_id;
                    $make_pickup_detai_where['qty'] = $this->data['qty'];
                    $res = insertData('make_pickup_detail',$make_pickup_detai_where);
                }

                // $where['area_id'] = $this->data['area_id'];
                // $where['pro_id'] = $this->data['pro_id'];
                // $where['prochar_id'] = $this->data['prochar_id'];
                // $where['warehouse_id'] = $this->data['warehouse_id'];

                // $oldQty = getValue('store2area',$where,'qty');
                // if($oldQty){
                //     $res = updateData('store2area',$where,['qty'=>$oldQty + $this->data['qty'],'volume'=>($oldQty + $this->data['qty']) * $this->data['m3'] ]);
                // }else{
                //     $insertData = $where;
                //     $insertData['qty'] = $this->data['qty'];
                //     $insertData['volume'] = $this->data['qty'] * $this->data['m3'];
                //     $res = insertData('store2area',$insertData);
                // }

            }else{
                $res = deleteData('make_pickup_detail',['sale_id'=>$this->data['sale_id'],'make_id'=>$this->data['make_id'],'pro_id'=>$this->data['pro_id'],'prochar_id'=>$this->data['prochar_id'],'warehouse_id'=>$this->data['warehouse_id'],'area_id'=>$this->data['area_id']]);
            }

            if($res >= 0){
                return json_encode(array('status'=>1,'msg'=>'通知成功'));
            }else{
                return json_encode(array('status'=>2,'msg'=>'通知失败'));
            }
        }else{
            return json_encode(array('status'=>2,'msg'=>'通知失败,通知数据不完善'));
        }
    }
    // 处理中 暂停
    function processPause()
    {
        if(isset($this->data['remark']) && $this->data['remark'] ){
            return $this->savePause('process');
        }else{
            return json_encode(array('status'=>2,'msg'=>'通知暂停失败sql'));
        }
    }

    function savePause($type='list')
    {   
        if($type == 'list'){
            $insertData['detail_id'] = $this->data['detail_id'];
            updateData('make_process_detail',['id'=>$this->data['detail_id']],['pause_id'=>$this->user_id,'pause_at'=>time()]);
        }else{
            $insertData['process_id'] = $this->data['process_id'];
            updateData('make_process',['id'=>$this->data['process_id']],['pause_id'=>$this->user_id,'pause_at'=>time()]);
            updateData('make_process_detail',['process_id'=>$this->data['process_id']],['pause_id'=>$this->user_id,'pause_at'=>time()]);
        }
        $insertData['remark'] = $this->data['remark'];
        $insertData['make_id'] = $this->data['make_id'];
        $insertData['warehouse_id'] = $this->data['warehouse_id'];        
        $insertData['create_id'] = $this->user_id;        
        $insertData['create_at'] = time();       
        $res = insertData('make_process_remark',$insertData); 

        // $this->saleModel->addSaleLog(getValue('make_process_detail',['id'=>$this->data['detail_id']],'sale_id'),'makePacker','包装主管通知暂停');
        if($res){
            return json_encode(array('status'=>1,'msg'=>'通知暂停成功'));
        }else{
            return json_encode(array('status'=>2,'msg'=>'通知暂停失败'));
        }
    }

    // 处理中 完成
    // make_process_store.qty 为负数是被扣除的数量，正数是累加的。
    function processFinish()
    {
        if($this->data['requestType'] == 'get'){
            $data = $this->getProItem($this->data,$this->lang,1);
            return json_encode($data);
        }else if($this->data['requestType'] == 'save'){
            $make_process_store['create_at'] = time();
            $make_process_store['create_id'] = $this->user_id;
            $make_process_store['process_id'] = $this->data['process_id'];
            $make_process_store['warehouse_id'] = $this->data['warehouse_id'];

            if( isset($this->data['proStore2area']) && !empty($this->data['proStore2area']) ){
                $pro_make_process_store = $make_process_store;
                $store2area_qty = $this->data['processQty'];
                foreach ($this->data['proStore2area'] as $k => $v) {
                    if($v['qty'] == 0) continue;
                    if($store2area_qty <= 0){
                        // continue;
                        break;
                    }else{
                        $pro_make_process_store['pro_id'] = $this->data['pro_id'];
                        $pro_make_process_store['area_id'] = $v['area_id'];
                        $pro_make_process_store['prochar_id'] = $this->data['prochar_id'];

                        if($v['qty'] - $store2area_qty > 0){
                            updateData('store2area',['id'=>$v['id']],['qty'=>$v['qty'] - $store2area_qty,'volume'=>($v['qty'] - $store2area_qty)*$this->data['prochar_m3']]);
                            $pro_make_process_store['qty'] = $store2area_qty-$store2area_qty*2;
                            $store2area_qty = 0; // 已扣完，跳出循环
                        }else{
                            updateData('store2area',['id'=>$v['id']],['qty'=>0,'volume'=>0]);
                            $pro_make_process_store['qty'] = $v['qty']-$v['qty']*2;
                            $store2area_qty = $this->data['processQty'] - $v['qty']; // 剩余，继续循环扣除
                        }
                        
                        insertData('make_process_store',$pro_make_process_store);
                        
                    }
                }
            }
            if( isset($this->data['itemData']) && !empty($this->data['itemData']) ){
                $proItem_make_process_store = $make_process_store;
                $processQty = $this->data['processQty'];
                foreach ($this->data['itemData'] as $k1 => $v1) {

                    $proItem_make_process_store['pro_id'] = $v1['pro_id'];
                    $proItem_make_process_store['prochar_id'] = $v1['prochar_id'];

                    $store2area_itemQty = $this->data['processQty']*$v1['qty'];
                    if(!empty($v1['itemStore2area'])){
                        foreach ($v1['itemStore2area'] as $k2 => $v2) {
                            if($v2['qty'] == 0) continue;
                            if($store2area_itemQty <= 0){
                                break;
                            }else{
                                $proItem_make_process_store['area_id'] = $v2['area_id'];
                                // 加工数量*配件与产品配比。
                                // $store2area_itemQty = $v2['qty'] - $processQty*$v1['qty'];
                                if($v2['qty'] - $store2area_itemQty > 0){
                                    updateData('store2area',['id'=>$v2['id']],['qty'=>$v2['qty'] - $store2area_itemQty,'volume'=>($v2['qty'] - -$store2area_qty)*$this->data['prochar_m3']]);
                                    $proItem_make_process_store['qty'] = $store2area_itemQty-$store2area_itemQty*2;
                                    $store2area_itemQty = 0;
                                }else{
                                    updateData('store2area',['id'=>$v2['id']],['qty'=>0,'volume'=>0]);
                                    $proItem_make_process_store['qty'] = $v2['qty']-$v2['qty']*2;
                                    $store2area_itemQty = $store2area_itemQty - $v2['qty'];
                                }
                                $res = insertData('make_process_store',$proItem_make_process_store);
                            }
                        }
                    }
                }
            }

            if(isset($this->data['processFinishStore2area_id'])){
                // $store2area_qty_ = $this->data['processQty'] - $this->data['processDetailQty'] > 0 ? $this->data['processQty'] - $this->data['processDetailQty'] : 0;
                $store2area_qty_ = $this->data['finishSaveQty'];
                if($store2area_qty_ > 0){
                    $getStore2area_qty = getValue('store2area',['id'=>$this->data['processFinishStore2area_id']],'qty');
                    if($getStore2area_qty > 0){
                        updateData('store2area',['id'=>$v['id']],['qty'=>$getStore2area_qty+$store2area_qty_,'volume'=>($store2area_qty_+$getStore2area_qty)*$this->data['prochar_m3']]);
                    }else{
                        insertData('store2area',['qty'=>$getStore2area_qty+$store2area_qty_,'volume'=>$store2area_qty_*$this->data['prochar_m3']]);
                    }

                    $make_process_store['pro_id'] = $this->data['pro_id'];
                    $make_process_store['prochar_id'] = $this->data['prochar_id'];
                    $make_process_store['area_id'] = $this->data['processFinishArea_id'];
                    $make_process_store['qty'] = $store2area_qty_;
                    insertData('make_process_store',$make_process_store);
                }
            }

            // 记录使用库存，是为了加工完成后撤消操作时，对库存复原
            $res = updateData('make_process',['id'=>$this->data['process_id']],['finish_id'=>$this->user_id,'finish_at'=>time()]);
            $res1 = updateData('make_process_detail',['process_id'=>$this->data['process_id']],['finish_at'=>time()]);

            // 判断当前$this->data['process_id'] 旗下所有的process_detail中相互关联的sale_make 是否 全部都已经finish_at > 0 ,
            // 同一个make_id的 process_detail.finish_at > 0 则编辑make_id.process_at > 0
            $make_ids = getDatas('make_process_detail',['make_id'],['process_id'=>$this->data['process_id']]);
            if(!empty($make_ids)){
                foreach ($make_ids as $k1 => $v1) {
                    if(!hasData('make_process_detail',['make_id'=>$v1['make_id'],'finish_at'=>0])){
                        updateData('sale_make',['id'=>$v1['make_id']],['process_at'=>time()]);
                        $this->saleModel->addSaleLog(getValue('sale_make',['id'=>$v1['make_id']],'sale_id'),'makePacker','包装主管处理完成');
                    }
                }
            }

            return json_encode(array('status'=>1,'msg'=>'完成加工'));
        }
    }

    // 提货取消
    function pickupCancel()
    {
        if($this->data['pickupId']){
            $res = updateData('make_pickup_detail',['id'=>$this->data['pickupId']],['cancel_at'=>time(),'cancel_id'=>$this->user_id]);
            if($res){
                return json_encode(array('status'=>1,'msg'=>'取消成功'));
            }else{
                return json_encode(array('status'=>2,'msg'=>'取消失败'));
            }
        }else{
            return json_encode(array('status'=>2,'msg'=>'请选择正确的数据取消'));
        }
    }

    // 提货 完成
    // 提货收到确认是给 make_pickup_detail.received_at=赋值并增加目的地的库存。
    function pickupProcess()
    {
        if($this->data['pickupDetail_id'] && $this->data['qty']){
            $res = updateData('make_pickup_detail',['id'=>$this->data['pickupDetail_id']],['received_at'=>time()]);
            if($res){
                $where = [
                            'pro_id'=>$this->data['pro_id'],
                            'prochar_id'=>$this->data['prochar_id'],
                            'warehouse_id'=>$this->data['warehouse_id'],
                            'area_id'=>$this->data['area_id']
                        ];
                $store2area_qty = getValue('store2area',$where,'qty');
                if($store2area_qty){
                    $res1 = updateData('store2area',$where,['qty'=>$this->data['qty']+$store2area_qty,'volume'=>($this->data['qty']+$store2area_qty)*$this->data['m3'] ]);
                }else{
                    $where['qty'] = $this->data['qty'];
                    $where['volume'] = $this->data['qty']*$this->data['m3'];
                    $res1 = insertData('store2area',$where);
                }
                if($res1 > 0){
                    return json_encode(array('status'=>1,'msg'=>'已确认收到'));
                }else{
                    return json_encode(array('status'=>2,'msg'=>'认收到失败'));
                }
            }else{
                return json_encode(array('status'=>2,'msg'=>'认收到失败,检查数据'));
            }
        }
    }

    //已完成，取消完成
    function finishCancel()
    {
        if(isset($this->data['process_id'])){
            $res = updateData('make_process',['id'=>$this->data['process_id']],['finish_at'=>0,'finish_id'=>0]);
            if($res){
                updateData('make_process_detail',['process_id'=>$this->data['process_id']],['finish_at'=>0]);
                $make_process_store = getDatas('make_process_store',['*'],['process_id'=>$this->data['process_id']]);
                if(!empty($make_process_store)){
                    foreach ($make_process_store as $k => $v) {

                        $where = [];
                        $where = ['warehouse_id'=>$v['warehouse_id'],'area_id'=>$v['area_id'],'pro_id'=>$v['pro_id'],'prochar_id'=>$v['prochar_id']];
                        $store2area = Db::name('store2area')->where($where)->find();

                        if($store2area){
                            if($v['qty'] >= 0){
                                $qty = $store2area['qty'] - $v['qty'];
                            }else{
                                $qty = $store2area['qty'] + abs($v['qty']);
                            }
                            updateData('store2area',$where,['qty'=>$qty,'volume'=>$qty*$this->data['m3']]);
                        }else{
                            $where['qty'] = abs($v['qty']);
                            $where['volume'] = abs($v['qty'])*$this->data['m3'];
                            insertData('store2area',$where);
                        }
                    }
                    deleteData('make_process_store',['process_id'=>$this->data['process_id']]);
                }

                return json_encode(array('status'=>1,'msg'=>'取消完成成功'));
            }else{
                return json_encode(array('status'=>2,'msg'=>'取消完成失败'));
            }
        }else{
            return json_encode(array('status'=>2,'msg'=>'请选择正确的数据进行操作'));
        }
    }

}
